package net.entelijan.cobean.examples.showcase.text;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.data.literature.LitBookSearchItem;
import net.entelijan.cobean.data.literature.OrderAttribute;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Test;

@SuppressWarnings("unused")
public class ValidatingTextFestTestCase {

	private Log log = LogFactory.getLog(ValidatingTextFestTestCase.class);

	@Test
	public void testValidInputToComponents() {
		SimpleMultiFailureFieldsTextPanel panel = new SimpleMultiFailureFieldsTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		ValidatingTextShowcase sc = new ValidatingTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			JTextComponentFixture nameFailureTextBox = showcaseFix
					.textBox("nameFailureTextField");
			JTextComponentFixture birthdayFailureTextBox = showcaseFix
					.textBox("birthdayFailureTextField");
			JTextComponentFixture idFailureTextBox = showcaseFix
					.textBox("idFailureTextField");
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			showcaseFix.button("resetButton");
			showcaseFix.button("uppercaseButton");
			showcaseFix.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox
					.requireText("'' is not between 01.01.1900 and 31.12.2050");
			idFailureTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertNull(sc.getBirthdayCobean().getValue());

			// Enter a new name
			nameTextBox.setText("DDD");
			idTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			pauseShort();
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertNull(sc.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox
					.requireText("'' is not between 01.01.1900 and 31.12.2050");
			idFailureTextBox.requireEmpty();

			// Enter a new valid ID
			idTextBox.setText("");
			idTextBox.enterText("10");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireEmpty();
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox
					.requireText("'' is not between 01.01.1900 and 31.12.2050");
			idFailureTextBox.requireEmpty();

			// Enter a valid birthdate
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.1.1999");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireText("01.01.1999");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "19990101"), sc
					.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox.requireEmpty();
			idFailureTextBox.requireEmpty();

			// Enter an invalid birthdate
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("01.01.1899");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireText("01.01.1899");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "19990101"), sc
					.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox
					.requireText("01.01.1899 is not between 01.01.1900 and 31.12.2050");
			idFailureTextBox.requireEmpty();

			// Enter an invalid birthdate
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("01.01.3000");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireText("01.01.3000");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "19990101"), sc
					.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox
					.requireText("01.01.3000 is not between 01.01.1900 and 31.12.2050");
			idFailureTextBox.requireEmpty();

			// Enter a valid birthdate
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.1.2000");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireText("01.01.2000");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20000101"), sc
					.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox.requireEmpty();
			idFailureTextBox.requireEmpty();

			// Enter an invalid id
			idTextBox.setText("");
			idTextBox.enterText("Not an ID");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("Not an ID");
			birthdayTextBox.requireText("01.01.2000");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20000101"), sc
					.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox.requireEmpty();
			idFailureTextBox.requireText("'Not an ID' is not a valid integer");
			
			// Enter another invalid id
			idTextBox.setText("");
			// TODO Check why fest does not wor with negative numbers. E.g. -123
			idTextBox.enterText("123");
			nameTextBox.focus();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("123");
			birthdayTextBox.requireText("01.01.2000");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20000101"), sc
					.getBirthdayCobean().getValue());
			nameFailureTextBox.requireEmpty();
			birthdayFailureTextBox.requireEmpty();
			idFailureTextBox.requireText("123 is not between 0 and 100");

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	private synchronized void pause(int pauseTime) {
		try {
			wait(pauseTime);
		} catch (InterruptedException e) {
			// Nothing to be done here
		}
	}

	@Test
	public void testValidInputToCobeans() {
		SimpleSingleFailureFieldTextPanel panel = new SimpleSingleFailureFieldTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		SimpleSingleFailureFieldTextShowcase sc = new SimpleSingleFailureFieldTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			showcaseFix.textBox("failureTextField");
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			showcaseFix.button("resetButton");
			showcaseFix.button("uppercaseButton");
			showcaseFix.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new name
			sc.getNameCobean().setValue("DDD");
			pauseShort();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new valid ID
			sc.getIdCobean().setValue(10L);
			pauseShort();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireEmpty();
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new valid birthdate
			sc.getBirthdayCobean().setValue(
					CobeanTestUtil.createDate("yyyyMMdd", "19990101"));
			pauseShort();
			nameTextBox.requireText("DDD");
			idTextBox.requireText("10");
			birthdayTextBox.requireText("01.01.1999");
			assertEquals("DDD", sc.getNameCobean().getValue());
			assertEquals(10, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "19990101"), sc
					.getBirthdayCobean().getValue());

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	@Test
	public void testInvalidID() {
		SimpleSingleFailureFieldTextPanel panel = new SimpleSingleFailureFieldTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		SimpleSingleFailureFieldTextShowcase sc = new SimpleSingleFailureFieldTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			final JTextComponentFixture failureTextBox = showcaseFix
					.textBox("failureTextField");
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			showcaseFix.button("resetButton");
			showcaseFix.button("uppercaseButton");
			showcaseFix.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new invalid ID
			assertEquals(Color.WHITE, idTextBox.component().getBackground());
			idTextBox.setText("");
			idTextBox.enterText("DDD");
			birthdayTextBox.focus();
			failureTextBox.requireText("'DDD' is not a valid integer");
			assertNotSame(Color.WHITE, idTextBox.component().getBackground());

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("DDD");
			birthdayTextBox.requireEmpty();

			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new valid ID
			idTextBox.setText("");
			idTextBox.enterText("7");
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());
			failureTextBox.requireEmpty();

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	@Test
	public void testResetWithInvalidID() {
		SimpleSingleFailureFieldTextPanel panel = new SimpleSingleFailureFieldTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		SimpleSingleFailureFieldTextShowcase sc = new SimpleSingleFailureFieldTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			final JTextComponentFixture failureTextBox = showcaseFix
					.textBox("failureTextField");
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			final JButtonFixture resetButton = showcaseFix
					.button("resetButton");
			showcaseFix.button("uppercaseButton");
			showcaseFix.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new invalid ID
			idTextBox.setText("");
			idTextBox.enterText("DDD");
			nameTextBox.focus();
			failureTextBox.requireText("'DDD' is not a valid integer");

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("DDD");
			birthdayTextBox.requireEmpty();
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new valid ID
			resetButton.click();
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());
			failureTextBox.requireEmpty();

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	@Test
	public void testInvalidBirthday() {
		SimpleSingleFailureFieldTextPanel panel = new SimpleSingleFailureFieldTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		SimpleSingleFailureFieldTextShowcase sc = new SimpleSingleFailureFieldTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			final JTextComponentFixture failureTextBox = showcaseFix
					.textBox("failureTextField");
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			showcaseFix.button("resetButton");
			showcaseFix.button("uppercaseButton");
			showcaseFix.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new invalid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("DDD");
			nameTextBox.focus();
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("DDD");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());
			failureTextBox
					.requireText("'DDD' is not a valid date. Use 'dd.MM.yyyy'");

			// Enter a new another invalid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.55.2009");
			nameTextBox.focus();
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("1.55.2009");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());
			failureTextBox
					.requireText("'1.55.2009' is not a valid date. Use 'dd.MM.yyyy'");

			// Enter a new valid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.2.2009");
			nameTextBox.focus();
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("01.02.2009");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20090201"), sc
					.getBirthdayCobean().getValue());
			failureTextBox.requireEmpty();

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	@Test
	public void testResetWithInvalidBirthday() {
		SimpleSingleFailureFieldTextPanel panel = new SimpleSingleFailureFieldTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		SimpleSingleFailureFieldTextShowcase sc = new SimpleSingleFailureFieldTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			final JTextComponentFixture failureTextBox = showcaseFix
					.textBox("failureTextField");
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			final JButtonFixture resetButton = showcaseFix
					.button("resetButton");
			showcaseFix.button("uppercaseButton");
			showcaseFix.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new invalid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("DDD");
			idTextBox.focus();
			failureTextBox
					.requireText("'DDD' is not a valid date. Use 'dd.MM.yyyy'");

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("DDD");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new another invalid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.55.2009");
			nameTextBox.focus();
			failureTextBox
					.requireText("'1.55.2009' is not a valid date. Use 'dd.MM.yyyy'");

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("1.55.2009");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			// Enter a new valid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.2.2009");
			idTextBox.focus();
			failureTextBox.requireEmpty();

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("01.02.2009");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20090201"), sc
					.getBirthdayCobean().getValue());

			// Enter a new another invalid birthday
			birthdayTextBox.setText("");
			birthdayTextBox.enterText("1.55.2009");
			idTextBox.focus();
			failureTextBox
					.requireText("'1.55.2009' is not a valid date. Use 'dd.MM.yyyy'");

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("1.55.2009");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20090201"), sc
					.getBirthdayCobean().getValue());

			// Enter a new valid birthday
			resetButton.click();
			pauseShort();
			failureTextBox.requireEmpty();
			failureTextBox.requireEmpty();

			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireText("01.02.2009");
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "20090201"), sc
					.getBirthdayCobean().getValue());

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	@Test
	public void testUpperLowercase() {
		SimpleSingleFailureFieldTextPanel panel = new SimpleSingleFailureFieldTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);

		SimpleSingleFailureFieldTextShowcase sc = new SimpleSingleFailureFieldTextShowcase();
		sc.setService(new TestLitService());
		sc.init(panel);

		showcaseFix.show();
		try {
			final JTextComponentFixture birthdayTextBox = showcaseFix
					.textBox("birthdayTextField");
			final JTextComponentFixture idTextBox = showcaseFix
					.textBox("idTextField");
			final JTextComponentFixture nameTextBox = showcaseFix
					.textBox("nameTextField");
			final JButtonFixture upperButton = showcaseFix
					.button("uppercaseButton");
			final JButtonFixture lowerButton = showcaseFix
					.button("lowercaseButton");

			// Check initial state
			nameTextBox.requireText("Gernot Wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();

			// Check if there are the same values in the cobeans
			assertEquals("Gernot Wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			upperButton.click();
			nameTextBox.requireText("GERNOT WOLFSGRUBER");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			assertEquals("GERNOT WOLFSGRUBER", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

			lowerButton.click();
			nameTextBox.requireText("gernot wolfsgruber");
			idTextBox.requireText("7");
			birthdayTextBox.requireEmpty();
			assertEquals("gernot wolfsgruber", sc.getNameCobean().getValue());
			assertEquals(7, sc.getIdCobean().getValue().intValue());
			assertEquals(null, sc.getBirthdayCobean().getValue());

		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	private synchronized void pauseShort() {
		try {
			wait(200);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private synchronized void pauseMedium() {
		try {
			wait(1000);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private synchronized void pauseLong() {
		try {
			wait(4000);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private static final class TestLitService implements ILitService {

		@Override
		public List<LitBook> loadAllBooks() {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public List<LitAuthor> loadAllAuthors() {
			ArrayList<LitAuthor> alist = new ArrayList<LitAuthor>();
			for (int i = 0; i < 10; i++) {
				LitAuthor auth = new LitAuthor();
				auth.setId(7);
				auth.setName("Gernot Wolfsgruber");
				alist.add(auth);
			}
			return alist;
		}

		@Override
		public void addBookToAuthor(LitAuthor author, LitBook book) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public LitAuthor loadAuthor(long id) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public LitBook loadBook(long id) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public void removeBookFromAuthor(LitAuthor author, LitBook book) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public void saveAuthor(LitAuthor author) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public void saveBook(LitBook book) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public List<LitBook> findBooks(LitBookSearchItem searchItem) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public long getBookCount() {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public List<LitBook> loadBooks(int fromIndex, int toIndex,
				List<OrderAttribute> order) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public void deleteAuthor(long id) {
			throw new IllegalStateException("Should not be called");
		}

		@Override
		public void deleteBook(long id) {
			throw new IllegalStateException("Should not be called");
		}
	}

}
